<!DOCTYPE HTML>
<html>

<head>
	<link rel="bookmark"  type="image/x-icon"  href="/img/logo_miccall.png"/>
	<link rel="shortcut icon" href="/img/logo_miccall.png">
	
			    <title>
    Ame雨
    </title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
    <link rel="stylesheet" href="/amelin-ting/css/mic_main.css" />
    <link rel="stylesheet" href="/amelin-ting/css/dropdownMenu.css" />
    <meta name="keywords" content="Ame雨" />
    
    	<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
	 
    <noscript>
        <link rel="stylesheet" href="/amelin-ting/css/noscript.css" />
    </noscript>
    <style type="text/css">
        body:before {
          content: ' ';
          position: fixed;
          top: 0;
          background: url('/amelin-ting/img/bg.jpg') center 0 no-repeat;
          right: 0;
          bottom: 0;
          left: 0;
          background-size: cover; 
        }
    </style>

			    
  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script async type="text/javascript" src="//cdn.bootcss.com/mathjax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
  


    <script src="/amelin-ting/js/jquery.min.js"></script>
    <script src="/amelin-ting/js/jquery.scrollex.min.js"></script>
    <script src="/amelin-ting/js/jquery.scrolly.min.js"></script>
    <script src="/amelin-ting/js/skel.min.js"></script>
    <script src="/amelin-ting/js/util.js"></script>
    <script src="/amelin-ting/js/main.js"></script>
	
<meta name="generator" content="Hexo 5.4.0"></head>
    
		
<!-- Layouts -->



<!--  代码渲染  -->
<link rel="stylesheet" href="/css/prism_coy.css" />
<link rel="stylesheet" href="/css/typo.css" />
<!-- 文章页 -->
<body class="is-loading">
    <!-- Wrapper 外包 s-->
    <div id="wrapper" class="fade-in">
        <!-- Intro 头部显示 s -->
        <!-- Intro 头部显示 e -->
        <!-- Header 头部logo start -->
        <header id="header">
    <a href="/amelin-ting/" class="logo">Art Space</a>
</header>
        <!-- Nav 导航条 start -->
        <nav id="nav" class="special" >
            <ul class="menu links" >
			<!-- Homepage  主页  --> 
			<li >
	            <a href="/amelin-ting/" rel="nofollow">主页</a>
	        </li>
			<!-- categories_name  分类   --> 
	        
	        <li class="active">
	            <a href="#s1">分类</a>
	                    <ul class="submenu">
	                        <li>
	                        <a class="category-link" href="/amelin-ting/categories/Go/">Go</a></li><li><a class="category-link" href="/amelin-ting/categories/django/">django</a></li><li><a class="category-link" href="/amelin-ting/categories/drf/">drf</a></li><li><a class="category-link" href="/amelin-ting/categories/gin/">gin</a></li><li><a class="category-link" href="/amelin-ting/categories/go/">go</a></li><li><a class="category-link" href="/amelin-ting/categories/linux/">linux</a></li><li><a class="category-link" href="/amelin-ting/categories/mysql/">mysql</a></li><li><a class="category-link" href="/amelin-ting/categories/python/">python</a></li><li><a class="category-link" href="/amelin-ting/categories/%E5%86%85%E7%BD%91%E7%A9%BF%E9%80%8F/">内网穿透</a></li><li><a class="category-link" href="/amelin-ting/categories/%E5%89%8D%E7%AB%AF/">前端</a>
	                    </ul>
	        </li>
	        
	        <!-- archives  归档   --> 
	        
	        
		        <!-- Pages 自定义   -->
		        
		        <li>
		            <a href="/about/" title="简历">
		                简历
		            </a>
		        </li>
		        
		        <li>
		            <a href="/group/" title="团队">
		                团队
		            </a>
		        </li>
		        
		        <li>
		            <a href="/gallery/" title="图库">
		                图库
		            </a>
		        </li>
		        
		        <li>
		            <a href="/tag/" title="标签">
		                标签
		            </a>
		        </li>
		        


            </ul>
            <!-- icons 图标   -->
			<ul class="icons">
                    
                    <li>
                        <a title="github" href="https://github.com/miccall" target="_blank" rel="noopener">
                            <i class="icon fa fa-github"></i>
                        </a>
                    </li>
                    
                    <li>
                        <a title="500px" href="http://500px.com" target="_blank" rel="noopener">
                            <i class="icon fa fa-500px"></i>
                        </a>
                    </li>
                    
			</ul>
</nav>

        <div id="main" >
            <div class ="post_page_title_img" style="height: 25rem;background-image: url(https://img2.baidu.com/it/u=421585645,4111401005&amp;fm=26&amp;fmt=auto);background-position: center; background-repeat:no-repeat; background-size:cover;-moz-background-size:cover;overflow:hidden;" >
                <a href="#" style="padding: 4rem 4rem 2rem 4rem ;"><h2 >python函数</h2></a>
            </div>
            <!-- Post -->
            <div class="typo" style="padding: 3rem;">
                <h1 id="函数"><a href="#函数" class="headerlink" title="函数"></a>函数</h1><h2 id="一、函数的定义"><a href="#一、函数的定义" class="headerlink" title="一、函数的定义"></a>一、函数的定义</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>、什么是函数</span><br><span class="line">	函数就是封装了某一功能的代码</span><br><span class="line"><span class="number">2</span>、为什么要函数</span><br><span class="line">	因为 不利于方便读</span><br><span class="line">    	 代码冗余</span><br><span class="line">         维护性差 拓展性也差</span><br><span class="line"><span class="number">3</span>、怎么用函数</span><br><span class="line">	先定义(三种方式)</span><br><span class="line">	后调用(三种方式)</span><br><span class="line">	返回值(三种方式)</span><br><span class="line">	</span><br></pre></td></tr></table></figure>



<h2 id="二、函数的定义与调用"><a href="#二、函数的定义与调用" class="headerlink" title="二、函数的定义与调用"></a>二、函数的定义与调用</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>、函数的定义模板</span><br><span class="line">	<span class="function"><span class="keyword">def</span> 函数名(<span class="params">参数<span class="number">1</span>， 参数<span class="number">2</span></span>)</span></span><br><span class="line"><span class="function">    	&quot;&quot;&quot;文档描述&quot;&quot;&quot;</span></span><br><span class="line"><span class="function">        函数数据</span></span><br><span class="line"><span class="function">        <span class="title">return</span> 值</span></span><br><span class="line"><span class="function">2、函数的调用    </span></span><br><span class="line"><span class="function"><span class="title">def</span> <span class="title">func</span>():</span>  <span class="comment"># func 为内存的地址 func()则为函数的值</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;666&quot;</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">func()</span><br></pre></td></tr></table></figure>

<h2 id="三、定义函数的三种形式"><a href="#三、定义函数的三种形式" class="headerlink" title="三、定义函数的三种形式"></a>三、定义函数的三种形式</h2><h3 id="3-1-无参函数"><a href="#3-1-无参函数" class="headerlink" title="3.1 无参函数"></a>3.1 无参函数</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">func</span>():</span>  <span class="comment"># func 为内存的地址 func()则为函数的值</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;666&quot;</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">func()</span><br></pre></td></tr></table></figure>

<h3 id="3-2-参数函数"><a href="#3-2-参数函数" class="headerlink" title="3.2 参数函数"></a>3.2 参数函数</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">func</span>(<span class="params">x, y</span>):</span>  <span class="comment"># x = 1, y = 2</span></span><br><span class="line">    <span class="built_in">print</span>(x, y)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">func(<span class="number">1</span>, <span class="number">2</span>)</span><br></pre></td></tr></table></figure>

<h3 id="3-3-空函数"><a href="#3-3-空函数" class="headerlink" title="3.3 空函数"></a>3.3 空函数</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">func</span>(<span class="params">x, y</span>):</span>  <span class="comment"># func 为内存的地址 func()则为函数的值</span></span><br><span class="line">    <span class="keyword">pass</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">func()</span><br></pre></td></tr></table></figure>

<h3 id="3-4-三大定义函数的应用场景"><a href="#3-4-三大定义函数的应用场景" class="headerlink" title="3.4 三大定义函数的应用场景"></a>3.4 三大定义函数的应用场景</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>、无参函数的应用场景</span><br><span class="line"><span class="number">2</span>、有参函数的应用场景</span><br><span class="line"><span class="number">3</span>、空函数的应用场景</span><br><span class="line">	当你做个项目的时候，写了很多函数确定功能的时候，可以用空函数先放那，然后根据实际情况再去修改</span><br></pre></td></tr></table></figure>



<h2 id="四、调用函数的三种形式"><a href="#四、调用函数的三种形式" class="headerlink" title="四、调用函数的三种形式"></a>四、调用函数的三种形式</h2><h3 id="4-1-语句的形式直接调用"><a href="#4-1-语句的形式直接调用" class="headerlink" title="4.1 语句的形式直接调用"></a>4.1 语句的形式直接调用</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 只调用函数</span></span><br><span class="line">func(x, y)</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h3 id="4-2-表达式调用"><a href="#4-2-表达式调用" class="headerlink" title="4.2 表达式调用"></a>4.2 表达式调用</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>、赋值表达式</span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">func</span>(<span class="params">x, y</span>):</span></span><br><span class="line">    res = x + y</span><br><span class="line">    <span class="keyword">return</span> res</span><br><span class="line"></span><br><span class="line">res = func(<span class="number">1</span>, <span class="number">2</span>)</span><br><span class="line"><span class="built_in">print</span>(res)</span><br><span class="line"></span><br><span class="line"><span class="number">2</span>、算术表达式</span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">func</span>(<span class="params">x, y</span>):</span></span><br><span class="line">    res = x + y</span><br><span class="line">    <span class="keyword">return</span> res</span><br><span class="line"></span><br><span class="line">res = func(<span class="number">1</span>, <span class="number">2</span>) * <span class="number">8</span></span><br><span class="line"><span class="built_in">print</span>(res)</span><br></pre></td></tr></table></figure>



<h3 id="4-3-函数调用可以当作参数"><a href="#4-3-函数调用可以当作参数" class="headerlink" title="4.3 函数调用可以当作参数"></a>4.3 函数调用可以当作参数</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">func</span>(<span class="params">x, y</span>):</span></span><br><span class="line">    res = x + y</span><br><span class="line">    <span class="keyword">return</span> res</span><br><span class="line">res = func(func(<span class="number">1</span>, <span class="number">2</span>),<span class="number">3</span>)</span><br><span class="line"><span class="built_in">print</span>(res)</span><br></pre></td></tr></table></figure>



<h2 id="五、返回值的三种形式"><a href="#五、返回值的三种形式" class="headerlink" title="五、返回值的三种形式"></a>五、返回值的三种形式</h2><h3 id="5-1-不返回"><a href="#5-1-不返回" class="headerlink" title="5.1 不返回"></a>5.1 不返回</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">func</span>(<span class="params">x, y</span>):</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;123&quot;</span>)</span><br></pre></td></tr></table></figure>



<h3 id="5-2-返回一个值"><a href="#5-2-返回一个值" class="headerlink" title="5.2 返回一个值"></a>5.2 返回一个值</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">func</span>(<span class="params">x, y</span>)</span></span><br><span class="line"><span class="function">    <span class="title">return</span> 10</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"><span class="title">res</span> = <span class="title">func</span>()</span></span><br><span class="line"><span class="function"><span class="title">print</span>(<span class="params">res</span>)</span></span><br></pre></td></tr></table></figure>

<h3 id="5-3-返回多个值"><a href="#5-3-返回多个值" class="headerlink" title="5.3 返回多个值"></a>5.3 返回多个值</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">func</span>(<span class="params">x, y</span>):</span></span><br><span class="line">    <span class="keyword">return</span> <span class="number">10</span>, <span class="number">8</span>, [<span class="number">1</span>, <span class="number">2</span>]</span><br><span class="line"></span><br><span class="line">res = func()</span><br><span class="line"><span class="built_in">print</span>(res)</span><br><span class="line"><span class="comment"># 会以元组的方式返回</span></span><br></pre></td></tr></table></figure>



<h2 id="六、形参与实参"><a href="#六、形参与实参" class="headerlink" title="六、形参与实参"></a>六、形参与实参</h2><h3 id="6-1-形参与实参"><a href="#6-1-形参与实参" class="headerlink" title="6.1 形参与实参"></a>6.1 形参与实参</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 形参是函数定义时括号内的变量名，接受外界的值</span></span><br><span class="line"><span class="comment"># 实参则是函数调用时括号里的 可以是常量 变量 表达式的或者三者混合的传入的值</span></span><br></pre></td></tr></table></figure>



<h3 id="6-2-位置参数"><a href="#6-2-位置参数" class="headerlink" title="6.2 位置参数"></a>6.2 位置参数</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>、什么是位置参数</span><br><span class="line">位置参数实际上就是按照从左到右的顺序去传参，参数可以是形参也可以是实参</span><br><span class="line">特点是 必须被传值 多一个不行 少一个不行</span><br><span class="line"><span class="number">2</span>、</span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">func</span>(<span class="params">x, y</span>) # <span class="title">x</span>, <span class="title">y</span>则是形参</span></span><br><span class="line"><span class="function">	<span class="title">print</span>(<span class="params"><span class="string">&quot;123&quot;</span></span>)</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">    </span></span><br><span class="line"><span class="function"><span class="title">func</span>(<span class="params"><span class="number">1</span>, <span class="number">2</span></span>)  # <span class="title">x</span> = 1, <span class="title">y</span> = 2,按照顺序传入了<span class="title">x</span>， <span class="title">y</span></span></span><br></pre></td></tr></table></figure>



<h3 id="6-3-关键字参数"><a href="#6-3-关键字参数" class="headerlink" title="6.3 关键字参数"></a>6.3 关键字参数</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>、什么是关键字参数</span><br><span class="line">关键字参数则是通过变量名 = 值 去准确传递，也可以说是关键字实参,这样就不用按照从左到右的顺序去传参数了</span><br><span class="line"><span class="number">2</span>、</span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">func</span>(<span class="params">x, y</span>)</span></span><br><span class="line"><span class="function">	<span class="title">print</span>(<span class="params">x, y</span>)</span></span><br><span class="line"><span class="function">    </span></span><br><span class="line"><span class="function">    </span></span><br><span class="line"><span class="function"><span class="title">func</span>(<span class="params">y=<span class="number">1</span>, x=<span class="number">2</span></span>)</span></span><br><span class="line"><span class="function">3、注意</span></span><br><span class="line"><span class="function">	3.1 位置参数必须在关键字参数前，例如</span></span><br><span class="line"><span class="function">        <span class="title">def</span> <span class="title">func</span>(<span class="params">x, y, z</span>)</span></span><br><span class="line"><span class="function">        <span class="title">print</span>(<span class="params">x, y, z</span>)</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">    <span class="title">func</span>(<span class="params"><span class="number">1</span>, z=<span class="number">2</span>, y=<span class="number">1</span></span>)</span></span><br><span class="line"><span class="function">    3.2 不能重复给一个形参传值</span></span><br></pre></td></tr></table></figure>



<h3 id="6-4-默认形参"><a href="#6-4-默认形参" class="headerlink" title="6.4  默认形参"></a>6.4  默认形参</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>、what</span><br><span class="line">	默认形参则是在定义函数时，就已经为形参赋值，这类形参称之为默认参数，当函数有多个参数时，需要将值经常改变的参数定义成位置参数，而将值改变较少的参数定义成默认参数。例如编写一个注册学生信息的函数，如果大多数学生的性别都为男，那完全可以将形参gender定义成默认参数</span><br><span class="line"><span class="number">2</span>、定义时就已经为参数gender赋值，意味着调用时可以不对gender赋值，这降低了函数调用的复杂度</span><br><span class="line">    <span class="comment"># 需要注意：</span></span><br><span class="line">        默认参数必须在位置参数之后</span><br><span class="line">        默认参数的值仅在函数定义阶段被赋值一次</span><br><span class="line"><span class="number">3</span>、代码</span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">student</span>(<span class="params">name, age, gender=<span class="string">&#x27;male&#x27;</span></span>):</span></span><br><span class="line">        <span class="built_in">print</span>(name, age, gender)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    student(<span class="string">&#x27;Ame&#x27;</span>, <span class="number">18</span>)</span><br><span class="line">    student(<span class="string">&#x27;Yuki&#x27;</span>, <span class="number">18</span>)</span><br><span class="line">    student(<span class="string">&#x27;Egon&#x27;</span>, <span class="number">18</span>, <span class="string">&#x27;female&#x27;</span>)</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h3 id="6-5-可变长参数"><a href="#6-5-可变长参数" class="headerlink" title="6.5 可变长参数"></a>6.5 可变长参数</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>what</span><br><span class="line">	可变长参数分为两种 一是可变长形参 二是可变可变长实参</span><br><span class="line"><span class="number">2.</span>可变长形参</span><br><span class="line">	我们可以用*args **kwargs来接受不固定的值的长度，比如我们不知道要传入多少参数，我们就可以用这两个来接受传入的参数</span><br><span class="line">    <span class="comment"># *args会传入后以元组的方式来保存</span></span><br><span class="line">    <span class="comment"># **kwargs参数传入后会以字典的方式去保存</span></span><br><span class="line"><span class="number">3.</span>可变长实参</span><br><span class="line">	<span class="comment"># 打散</span></span><br><span class="line">    我们用元组传入后 元组的数据会以打散后位置参数传入形参</span><br><span class="line">    字典方式传入后，字典就会以关键字实参模式传入形参</span><br><span class="line"><span class="number">4.</span>代码<span class="number">1</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">func</span>(<span class="params">*args, **kwargs</span>):</span></span><br><span class="line">        <span class="built_in">print</span>(args, kwargs)</span><br><span class="line">    func(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">4</span>, x=<span class="number">1</span>, y=<span class="number">2</span>, z=<span class="number">3</span>, c=<span class="number">4</span>)</span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">    (1, 2, 3, 4, 5, 4) &#123;&#x27;x&#x27;: 1, &#x27;y&#x27;: 2, &#x27;z&#x27;: 3, &#x27;c&#x27;: 4&#125;</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line"><span class="number">5.</span>代码<span class="number">2</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">index</span>(<span class="params">x, y, z</span>):</span></span><br><span class="line">        <span class="built_in">print</span>(x, y, z)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">wrapper</span>(<span class="params">*args, **kwargs</span>):</span></span><br><span class="line">        index(*args, **kwargs)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    wrapper(<span class="number">1</span>, z=<span class="number">2</span>, y=<span class="number">3</span>)</span><br><span class="line"><span class="comment"># 分析：首先将实参传值给形参：*args = (1,)  **kwargs = &#123;&#x27;y&#x27;:3,&#x27;z&#x27;:2&#125;</span></span><br><span class="line"><span class="comment"># 第二步：这里是直接把wrapper(*args,**kwargs)中的形参给到了index(*args,      **kwargs)的实参，</span></span><br><span class="line"><span class="comment"># 即index(*(1,),**&#123;&#x27;y&#x27;:3,&#x27;z&#x27;:2&#125;),这里再次将其打散，然后绑定给x,y,z</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h2 id="七、名称空间与作用域"><a href="#七、名称空间与作用域" class="headerlink" title="七、名称空间与作用域"></a>七、名称空间与作用域</h2><h3 id="7-1-名称空间"><a href="#7-1-名称空间" class="headerlink" title="7.1 名称空间"></a>7.1 名称空间</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">	名称空间是  在内存栈区中分好不同的名称空间，这样就可以存放一样的变量名的空间，所以因为有名称空间的存在，我们才可以在不同的空间去使用相同的名字</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br></pre></td></tr></table></figure>

<h3 id="7-2-内置名称空间"><a href="#7-2-内置名称空间" class="headerlink" title="7.2 内置名称空间"></a>7.2 内置名称空间</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">存放的名字： 从python解释器内置存放的名字</span></span><br><span class="line"><span class="string">存活的周期： 从python打开即生成    python关闭则死亡	   </span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="comment"># 例如</span></span><br><span class="line">    <span class="built_in">print</span></span><br><span class="line">    <span class="built_in">input</span></span><br></pre></td></tr></table></figure>

<h3 id="7-3-全局名称空间"><a href="#7-3-全局名称空间" class="headerlink" title="7.3 全局名称空间"></a>7.3 全局名称空间</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">存放的空间： 既不在函数内，也不再解释器内置里,都为全局名称空间</span></span><br><span class="line"><span class="string">存活的周期： 文件的执行即开始，文件的执行结束为死亡</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="comment"># 例如1</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">func</span>()</span></span><br><span class="line"><span class="function">        <span class="title">x</span> = 5</span></span><br><span class="line"><span class="function">        <span class="title">y</span> = 1</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">    <span class="title">func</span>()  # <span class="title">func</span>既不是函数内 也不是内置内，所以为全局名称空间，其他的都一样</span></span><br><span class="line"><span class="function"># 例如2</span></span><br><span class="line"><span class="function">    <span class="title">x</span> = 250</span></span><br><span class="line"><span class="function">    <span class="title">y</span> = 38</span></span><br><span class="line"><span class="function">    &quot;&quot;&quot;</span></span><br><span class="line"><span class="function">    <span class="title">x</span>和<span class="title">y</span>也是全局名称空间里面的，全局也皆可调用</span></span><br><span class="line"><span class="function">    &quot;&quot;&quot;</span></span><br><span class="line"><span class="function">    # 例如3</span></span><br><span class="line"><span class="function">    <span class="title">if</span> 3&gt;1:</span></span><br><span class="line">        c = <span class="number">10</span></span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">    这个c也是全局名称空间里面的，虽然他不是顶格写的，但是也是全部皆可调用</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h3 id="7-4-局部名称空间"><a href="#7-4-局部名称空间" class="headerlink" title="7.4 局部名称空间"></a>7.4 局部名称空间</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">存放的空间： 是函数内创造的</span></span><br><span class="line"><span class="string">存货的周期： 函数调用开始出生  函数调用结束死亡</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="comment"># 例如</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">func</span>()  # <span class="title">x</span>, <span class="title">y</span>则在局部名称空间 定义时出生</span></span><br><span class="line"><span class="function">        <span class="title">x</span> = 5</span></span><br><span class="line"><span class="function">        <span class="title">y</span> = 1</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">    <span class="title">func</span>()  # 调用时死亡</span></span><br></pre></td></tr></table></figure>

<h3 id="7-5-名称空间的出生顺序和销毁顺序和查找顺序"><a href="#7-5-名称空间的出生顺序和销毁顺序和查找顺序" class="headerlink" title="7.5 名称空间的出生顺序和销毁顺序和查找顺序"></a>7.5 名称空间的出生顺序和销毁顺序和查找顺序</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>出生顺序</span><br><span class="line">内置名称空间&gt;全局名称空间&gt;局部名称空间</span><br><span class="line"><span class="number">2.</span>销毁顺序</span><br><span class="line">局部名称空间&gt;全局名称空间&gt;内置名称空间</span><br><span class="line"><span class="number">3.</span>查找顺序</span><br><span class="line"><span class="comment"># 如果在局部名称空间</span></span><br><span class="line">局部名称空间--------全局名称空间--------内置名称空间</span><br><span class="line"><span class="comment"># 如果在全局名称空间</span></span><br><span class="line">全局名称空间--------内置名称空间</span><br></pre></td></tr></table></figure>

<h3 id="7-6-全局作用域"><a href="#7-6-全局作用域" class="headerlink" title="7.6 全局作用域"></a>7.6 全局作用域</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">全局存活</span></span><br><span class="line"><span class="string">全局有效： 即所有的函数都能调用他</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br></pre></td></tr></table></figure>



<h3 id="7-7-局部作用域"><a href="#7-7-局部作用域" class="headerlink" title="7.7 局部作用域"></a>7.7 局部作用域</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">局部存活</span></span><br><span class="line"><span class="string">局部有效: 即只有函数内才能调用他</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br></pre></td></tr></table></figure>



<h2 id="八、函数对象"><a href="#八、函数对象" class="headerlink" title="八、函数对象"></a>八、函数对象</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 函数对象的特点精髓就是可以把函数看成变量去调用</span></span><br><span class="line">比如</span><br><span class="line">	<span class="function"><span class="keyword">def</span> <span class="title">func</span>(<span class="params">x</span>):</span></span><br><span class="line">    	<span class="built_in">print</span>(x)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">	res = func()</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h2 id="九、-函数嵌套"><a href="#九、-函数嵌套" class="headerlink" title="九、 函数嵌套"></a>九、 函数嵌套</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 函数嵌套调用就是在一个函数内再调用一个函数</span></span><br><span class="line"><span class="comment"># 函数嵌套定义就是在一个函数内再定义一个函数</span></span><br><span class="line">	<span class="function"><span class="keyword">def</span> <span class="title">max1</span>(<span class="params">x, y</span>)</span></span><br><span class="line"><span class="function">    	<span class="title">if</span> <span class="title">x</span> &gt; <span class="title">y</span>:</span></span><br><span class="line">            <span class="keyword">return</span> x</span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            <span class="keyword">return</span> y</span><br><span class="line">        </span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">max</span>(<span class="params">a, b, c, d</span>):</span></span><br><span class="line">        res1 = max1(a, b)</span><br><span class="line">        res2 = max1(res1, c)</span><br><span class="line">        res3 = max1(res2, d)</span><br><span class="line">        <span class="built_in">print</span>(res3)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="built_in">max</span>(<span class="number">1</span>, <span class="number">156</span>, <span class="number">31</span>, <span class="number">456</span>)</span><br></pre></td></tr></table></figure>



<h2 id="十、闭包函数"><a href="#十、闭包函数" class="headerlink" title="十、闭包函数"></a>十、闭包函数</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.1</span>.什么是闭包函数</span><br><span class="line">	闭包函数 = 名称空间与作用域 + 函数对象 + 函数嵌套</span><br><span class="line">    其实闭包函数就是一个传参方式</span><br><span class="line">    </span><br><span class="line"><span class="number">1.2</span>.为什么要用闭包函数</span><br><span class="line">	万一函数的参数被占用了，我们应该怎么传参就用闭包函数</span><br><span class="line">    </span><br><span class="line"><span class="number">1.3</span>.怎么用闭包函数</span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">func</span>(<span class="params">x</span>):</span></span><br><span class="line">        x = <span class="number">100</span></span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">def</span> <span class="title">wrapper</span>():</span></span><br><span class="line">            <span class="built_in">print</span>(x)</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> wrapper</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    f = func()</span><br><span class="line">    f()</span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">    这就是闭包函数，wrapper被包起来了，只能用到func的x，外界都没有用</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br></pre></td></tr></table></figure>



<h2 id="十一、装饰器"><a href="#十一、装饰器" class="headerlink" title="十一、装饰器"></a>十一、装饰器</h2><h3 id="11-1、-装饰器的定义（what-why-how）"><a href="#11-1、-装饰器的定义（what-why-how）" class="headerlink" title="11.1、 装饰器的定义（what why how）"></a>11.1、 装饰器的定义（what why how）</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>什么是装饰器</span><br><span class="line">	装饰器就是装饰某一个东西的工具</span><br><span class="line"><span class="number">2.</span>为什么要用装饰器</span><br><span class="line">	因为在源函数代码不改变调用方法&amp;原代码给原函数添加新功能，装饰器是最好的解决工具</span><br><span class="line"><span class="number">3.</span>怎么用装饰器</span><br><span class="line">	详见下面</span><br></pre></td></tr></table></figure>

<h3 id="11-2、-装饰器的实现思路"><a href="#11-2、-装饰器的实现思路" class="headerlink" title="11.2、 装饰器的实现思路"></a>11.2、 装饰器的实现思路</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">	现有一个原函数index，想要为其添加一个时间差的功能，我们应该怎么做呢</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 方案一  这就已经修改了他的原代码了</span></span><br><span class="line">	<span class="keyword">import</span> time</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">index</span>(<span class="params">x</span>):</span></span><br><span class="line">        start_time = time.time()</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&#x27;welcome to index <span class="subst">&#123;x&#125;</span>&#x27;</span>)</span><br><span class="line">        time.sleep(<span class="number">3</span>)</span><br><span class="line">        <span class="built_in">print</span>(time.time() - start_time)</span><br><span class="line"></span><br><span class="line">    index(<span class="number">1</span>)</span><br><span class="line"><span class="comment"># 方案二 没有修改原函数的代码&amp;调用，但是代码冗余</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">index</span>(<span class="params">x</span>):</span></span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&#x27;welcome to index <span class="subst">&#123;x&#125;</span>&#x27;</span>)</span><br><span class="line">        time.sleep(<span class="number">3</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    start_time = time.time()</span><br><span class="line">    index(<span class="number">1</span>)</span><br><span class="line">    <span class="built_in">print</span>(time.time() - start_time)</span><br><span class="line"></span><br><span class="line">    start_time = time.time()</span><br><span class="line">    index(<span class="number">1</span>)</span><br><span class="line">    <span class="built_in">print</span>(time.time() - start_time)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 方案三 解决了代码冗余问题，但是原函数的调用我们修改了，而且wrapper写死了，只能调用index</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">index</span>(<span class="params">x</span>):</span></span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&#x27;welcome to index <span class="subst">&#123;x&#125;</span>&#x27;</span>)</span><br><span class="line">        time.sleep(<span class="number">3</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">wrapper</span>():</span></span><br><span class="line">        start_time = time.time()</span><br><span class="line">        index(<span class="number">1</span>)</span><br><span class="line">        <span class="built_in">print</span>(time.time() - start_time)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    wrapper()</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 方案四 我们先搞定调用index函数写死问题</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">index</span>(<span class="params">x</span>):</span></span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&#x27;welcome to index <span class="subst">&#123;x&#125;</span>&#x27;</span>)</span><br><span class="line">        time.sleep(<span class="number">3</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">outter</span>(<span class="params">func</span>):</span></span><br><span class="line">        <span class="function"><span class="keyword">def</span> <span class="title">wrapper</span>():</span></span><br><span class="line">            start_time = time.time()</span><br><span class="line">            func(<span class="number">1</span>)</span><br><span class="line">            <span class="built_in">print</span>(time.time() - start_time)</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> wrapper</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    index = outter(index)</span><br><span class="line">    index()</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 方案五  然后在搞定index函数参数问题</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">index</span>(<span class="params">x</span>):</span></span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&#x27;welcome to index <span class="subst">&#123;x&#125;</span>&#x27;</span>)</span><br><span class="line">        time.sleep(<span class="number">3</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">outter</span>(<span class="params">func</span>):</span></span><br><span class="line">        <span class="function"><span class="keyword">def</span> <span class="title">wrapper</span>(<span class="params">*args, **kwargs</span>):</span></span><br><span class="line">            start_time = time.time()</span><br><span class="line">            func(*args, **kwargs)</span><br><span class="line">            <span class="built_in">print</span>(time.time() - start_time)</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> wrapper</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    index = outter(index)</span><br><span class="line">    index(<span class="number">11</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 方案六  在搞定index返回值的问题，这样就把wrapper伪装成index伪装的很好了</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">index</span>(<span class="params">x</span>):</span></span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&#x27;welcome to index <span class="subst">&#123;x&#125;</span>&#x27;</span>)</span><br><span class="line">        time.sleep(<span class="number">3</span>)</span><br><span class="line">        <span class="keyword">return</span> <span class="number">123</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">outter</span>(<span class="params">func</span>):</span></span><br><span class="line">        <span class="function"><span class="keyword">def</span> <span class="title">wrapper</span>(<span class="params">*args, **kwargs</span>):</span></span><br><span class="line">            start_time = time.time()</span><br><span class="line">            res = func(*args, **kwargs)</span><br><span class="line">            <span class="built_in">print</span>(time.time() - start_time)</span><br><span class="line">            <span class="keyword">return</span> res</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> wrapper</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    index = outter(index)</span><br><span class="line">    res = index(<span class="number">11</span>)</span><br><span class="line">	<span class="built_in">print</span>(res)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 方案七 在让wrapper更像index一点</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">index</span>(<span class="params">x</span>):</span></span><br><span class="line">        <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">        这是index的配置信息</span></span><br><span class="line"><span class="string">        &quot;&quot;&quot;</span></span><br><span class="line"></span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&#x27;welcome to index <span class="subst">&#123;x&#125;</span>&#x27;</span>)</span><br><span class="line">        time.sleep(<span class="number">3</span>)</span><br><span class="line">        <span class="keyword">return</span> <span class="number">123</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">outter</span>(<span class="params">func</span>):</span></span><br><span class="line">        <span class="function"><span class="keyword">def</span> <span class="title">wrapper</span>(<span class="params">*args, **kwargs</span>):</span></span><br><span class="line">            start_time = time.time()</span><br><span class="line">            res = func(*args, **kwargs)</span><br><span class="line">            <span class="built_in">print</span>(time.time() - start_time)</span><br><span class="line">            <span class="keyword">return</span> res</span><br><span class="line"></span><br><span class="line">        wrapper.__name__ = index.__name__</span><br><span class="line">        wrapper.__doc__ = index.__doc__</span><br><span class="line">        <span class="keyword">return</span> wrapper</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    index = outter(index)</span><br><span class="line">    res = index(<span class="number">11</span>)</span><br><span class="line">    <span class="built_in">print</span>(res)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 语法糖</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">outter</span>(<span class="params">func</span>):</span></span><br><span class="line">        <span class="keyword">from</span> functools <span class="keyword">import</span> wraps</span><br><span class="line"><span class="meta">        @wraps(<span class="params">func</span>)</span></span><br><span class="line">        <span class="function"><span class="keyword">def</span> <span class="title">wrapper</span>(<span class="params">*args, **kwargs</span>):</span></span><br><span class="line">            start_time = time.time()</span><br><span class="line">            res = func(*args, **kwargs)</span><br><span class="line">            <span class="built_in">print</span>(time.time() - start_time)</span><br><span class="line">            <span class="keyword">return</span> res</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> wrapper</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">    @outter</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">index</span>(<span class="params">x</span>):</span></span><br><span class="line">        <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">        这是index的配置信息</span></span><br><span class="line"><span class="string">        &quot;&quot;&quot;</span></span><br><span class="line"></span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&#x27;welcome to index <span class="subst">&#123;x&#125;</span>&#x27;</span>)</span><br><span class="line">        time.sleep(<span class="number">3</span>)</span><br><span class="line">        <span class="keyword">return</span> <span class="number">123</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    res = index(<span class="number">11</span>)</span><br><span class="line">    <span class="built_in">print</span>(res)</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="11-3、装饰器的总结模板"><a href="#11-3、装饰器的总结模板" class="headerlink" title="11.3、装饰器的总结模板"></a>11.3、装饰器的总结模板</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 无参装饰器</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">outter</span>(<span class="params">func</span>):</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">wrapper</span>(<span class="params">*args, **kwargs</span>):</span></span><br><span class="line">        res = func(*args, **kwargs)</span><br><span class="line">        <span class="keyword">return</span> res</span><br><span class="line">    <span class="keyword">return</span> wrapper</span><br><span class="line"></span><br><span class="line"><span class="comment"># 有参装饰器</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">outter</span>(<span class="params">x</span>):</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">foo</span>(<span class="params">func</span>):</span></span><br><span class="line">        <span class="function"><span class="keyword">def</span> <span class="title">wrapper</span>(<span class="params">*args, **kwargs</span>):</span></span><br><span class="line">            res = func(*args, **kwargs)</span><br><span class="line">            <span class="keyword">return</span> res</span><br><span class="line">        <span class="keyword">return</span> wrapper</span><br><span class="line">   	<span class="keyword">return</span> foo</span><br></pre></td></tr></table></figure>

<h3 id="11-4-多个装饰器"><a href="#11-4-多个装饰器" class="headerlink" title="11.4 多个装饰器"></a>11.4 多个装饰器</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> time</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">deco1</span>(<span class="params">func</span>):</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;----deco1&#x27;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">wrapper</span>(<span class="params">*args, **kwargs</span>):</span></span><br><span class="line">        start_time = time.time()</span><br><span class="line">        res = func(*args, **kwargs)</span><br><span class="line">        <span class="built_in">print</span>(time.time() - start_time)</span><br><span class="line">        <span class="keyword">return</span> res</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> wrapper()</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">deco2</span>(<span class="params">func</span>):</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;----deco2&#x27;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">wrapper</span>(<span class="params">*args, **kwargs</span>):</span></span><br><span class="line">        start_time = time.time()</span><br><span class="line">        res = func(*args, **kwargs)</span><br><span class="line">        <span class="built_in">print</span>(time.time() - start_time)</span><br><span class="line">        <span class="keyword">return</span> res</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> wrapper()</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">@deco1</span></span><br><span class="line"><span class="meta">@deco2</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">index</span>(<span class="params">x</span>):</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&#x27;<span class="subst">&#123;x&#125;</span>&#x27;</span>)</span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">	当多个装饰器叠加的时候 例如这样是先从上而下去执行，但是返回值则是从下而上的去返回 所以deco2的返回值是在deco1，deco1的返回值是在真正的index上</span></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br></pre></td></tr></table></figure>



<h2 id="十二、-有参装饰器"><a href="#十二、-有参装饰器" class="headerlink" title="十二、 有参装饰器"></a>十二、 有参装饰器</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">login</span>():</span></span><br><span class="line">    name = <span class="built_in">input</span>(<span class="string">&#x27;name is :&#x27;</span>).strip()</span><br><span class="line">    pwd = <span class="built_in">input</span>(<span class="string">&#x27;pwd is :&#x27;</span>).strip()</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> name == <span class="string">&#x27;ycc&#x27;</span> <span class="keyword">and</span> pwd == <span class="string">&#x27;123&#x27;</span>:</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&#x27;login successful&#x27;</span>)</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&#x27;name or pwd error&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">auth</span>(<span class="params">db_type</span>):</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">deco</span>(<span class="params">func</span>):</span></span><br><span class="line">        <span class="function"><span class="keyword">def</span> <span class="title">wrapper</span>(<span class="params">*args,**kwargs</span>):</span></span><br><span class="line"></span><br><span class="line">            <span class="keyword">if</span> db_type == <span class="string">&#x27;file&#x27;</span>:</span><br><span class="line">                login()</span><br><span class="line">                <span class="built_in">print</span>(<span class="string">&#x27;基于文件验证&#x27;</span>)</span><br><span class="line">                <span class="comment"># res = func(*args,**kwargs)</span></span><br><span class="line">                <span class="comment"># return res</span></span><br><span class="line">            <span class="keyword">elif</span> db_type == <span class="string">&#x27;mysql&#x27;</span>:</span><br><span class="line">                login()</span><br><span class="line">                <span class="built_in">print</span>(<span class="string">&#x27;基于数据库验证&#x27;</span>)</span><br><span class="line">                <span class="comment"># res = func(*args, **kwargs)</span></span><br><span class="line">                <span class="comment"># return res</span></span><br><span class="line"></span><br><span class="line">            <span class="keyword">elif</span> db_type == <span class="string">&#x27;ldap&#x27;</span>:</span><br><span class="line">                login()</span><br><span class="line">                <span class="built_in">print</span>(<span class="string">&#x27;基于ldap验证&#x27;</span>)</span><br><span class="line">                <span class="comment"># res = func(*args, **kwargs)</span></span><br><span class="line">                <span class="comment"># return res</span></span><br><span class="line">            res = func(*args, **kwargs)</span><br><span class="line">            <span class="keyword">return</span> res</span><br><span class="line">        <span class="keyword">return</span> wrapper</span><br><span class="line">    <span class="keyword">return</span> deco</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">@auth(<span class="params">db_type=<span class="string">&#x27;file&#x27;</span></span>)</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">index</span>(<span class="params">x, y</span>):</span></span><br><span class="line">    <span class="built_in">print</span>(x, y)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">@auth(<span class="params">db_type=<span class="string">&#x27;mysql&#x27;</span></span>)</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">home</span>(<span class="params">name</span>):</span></span><br><span class="line">    <span class="comment"># print(&#x27;welcome %s to home page&#x27;%name)</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&#x27;welcome <span class="subst">&#123;name&#125;</span> to home page&#x27;</span>)</span><br><span class="line">    <span class="keyword">return</span> name</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">@auth(<span class="params">db_type=<span class="string">&#x27;ldap&#x27;</span></span>)</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">transfer</span>():</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;transfer&#x27;</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">index(<span class="number">1</span>, <span class="number">2</span>)</span><br><span class="line">name = home(<span class="string">&#x27;ycc&#x27;</span>)</span><br><span class="line"><span class="built_in">print</span>(name)</span><br></pre></td></tr></table></figure>



<h2 id="十三、可迭代对象与迭代器对象"><a href="#十三、可迭代对象与迭代器对象" class="headerlink" title="十三、可迭代对象与迭代器对象"></a>十三、可迭代对象与迭代器对象</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>什么是可迭代对象</span><br><span class="line">	里面有内置的__iter__方法的就是可迭代对象</span><br><span class="line">    <span class="string">&#x27;abc&#x27;</span>.__iter__</span><br><span class="line">    [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>].__iter__</span><br><span class="line">    &#123;<span class="string">&#x27;x&#x27;</span>:<span class="number">1</span>, <span class="string">&#x27;y&#x27;</span>:<span class="number">2</span>&#125;.__iter__</span><br><span class="line"><span class="comment"># 调用一个可迭代对象的__iter__()会得到一个返回值，改返回值是迭代器对象</span></span><br><span class="line"><span class="number">2.</span>迭代对象对象的特点：</span><br><span class="line">	<span class="number">2.1</span> 内置有__next__方法</span><br><span class="line">    iter_s = s.__iter__()  <span class="comment"># iter_s = iter(s)</span></span><br><span class="line">    <span class="built_in">print</span>(iter_s.__next__())</span><br><span class="line">    </span><br><span class="line">    dic = &#123;<span class="string">&#x27;x&#x27;</span>:<span class="number">1</span>, <span class="string">&#x27;y&#x27;</span>:<span class="number">2</span>&#125;</span><br><span class="line">    iter_dic = <span class="built_in">iter</span>(dic)</span><br><span class="line">    <span class="built_in">print</span>(iter_dic.__next__())</span><br></pre></td></tr></table></figure>



<h2 id="十四、迭代器"><a href="#十四、迭代器" class="headerlink" title="十四、迭代器"></a>十四、迭代器</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>什么是迭代器</span><br><span class="line">    迭代器就是用来迭代取值的工具</span><br><span class="line"></span><br><span class="line">    什么是迭代？</span><br><span class="line">        迭代就是一个重复的过程，但是每次重复都是基于上一次的结果进行的</span><br><span class="line"></span><br><span class="line">        <span class="comment"># 单纯的重复不叫迭代</span></span><br><span class="line">        <span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">            <span class="number">1</span>+<span class="number">1</span></span><br><span class="line"></span><br><span class="line">        <span class="comment"># 下面才是迭代的过程</span></span><br><span class="line">        count = <span class="number">1</span></span><br><span class="line">        <span class="keyword">while</span> count &lt; <span class="number">5</span>:</span><br><span class="line">            <span class="built_in">print</span>(count)</span><br><span class="line">            count += <span class="number">1</span></span><br><span class="line"></span><br><span class="line"><span class="number">2.</span>为何要用迭代器</span><br><span class="line">    优点</span><br><span class="line">    (<span class="number">1</span>) 找到一种通用的迭代取值方案-&gt;<span class="keyword">for</span>循环</span><br><span class="line">    （<span class="number">2</span>）惰性计算，节省内存</span><br><span class="line"></span><br><span class="line">    缺点：</span><br><span class="line">        <span class="number">1</span>、不能取指定的值，只能往后<span class="built_in">next</span></span><br><span class="line">        <span class="number">2</span>、迭代器对象是一次性的：一个迭代器对象值取干净了，不能继续取了</span><br><span class="line"></span><br><span class="line"><span class="number">3.</span>可迭代对象</span><br><span class="line">    <span class="number">3.1</span> 内置有__iter__方法的对象都是可迭代对象</span><br><span class="line">            <span class="string">&quot;abc&quot;</span>.__iter__()</span><br><span class="line">            [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>].__iter__()</span><br><span class="line">            (<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>).__iter__()</span><br><span class="line">            &#123;<span class="string">&quot;x&quot;</span>: <span class="number">1</span>, <span class="string">&quot;y&quot;</span>: <span class="number">2</span>&#125;.__iter__()</span><br><span class="line">            &#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>&#125;.__iter__()</span><br><span class="line">            <span class="built_in">open</span>(<span class="string">&#x27;a.txt&#x27;</span>, mode=<span class="string">&#x27;r&#x27;</span>).__iter__()</span><br><span class="line">            <span class="built_in">open</span>(<span class="string">&#x27;a.txt&#x27;</span>, mode=<span class="string">&#x27;r&#x27;</span>).__next__()</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 调用可迭代对象的__iter__()会得到一个返回值，该返回值是迭代器对象</span></span><br><span class="line">迭代对象对象的特点：</span><br><span class="line">          <span class="number">1</span>、内置有__next__()方法</span><br><span class="line">          <span class="number">2</span>、内置有__iter__()方法</span><br><span class="line"></span><br><span class="line">    s = <span class="string">&quot;abc&quot;</span></span><br><span class="line">    iter_s = s.__iter__()  <span class="comment"># iter_s = iter(s)</span></span><br><span class="line">    <span class="built_in">print</span>(iter_s.__next__())  <span class="comment"># print(next(ites_s))</span></span><br><span class="line">    <span class="built_in">print</span>(iter_s.__next__())</span><br><span class="line">    <span class="built_in">print</span>(iter_s.__next__())</span><br><span class="line">    <span class="built_in">print</span>(iter_s.__next__())</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    dic = &#123;<span class="string">&quot;x&quot;</span>:<span class="number">1</span>,<span class="string">&quot;y&quot;</span>:<span class="number">2</span>&#125;</span><br><span class="line">    iter_dic = dic.__iter__()</span><br><span class="line">    <span class="built_in">print</span>(iter_dic.__next__())</span><br><span class="line">    <span class="built_in">print</span>(iter_dic.__next__())</span><br><span class="line"></span><br><span class="line"><span class="number">4.</span> <span class="keyword">for</span>工作原理</span><br><span class="line">    <span class="number">4.1</span> 调用<span class="keyword">in</span>后那个对象的__iter__方法，得到一个迭代器对象iter_names</span><br><span class="line">    <span class="number">4.2</span> x=<span class="built_in">next</span>(iter_names),然后运行循环体代码</span><br><span class="line">    <span class="number">4.3</span> 重复步骤<span class="number">2</span>，直到取完值，抛出异常，<span class="keyword">for</span>循环会帮我们捕捉异常结束循环</span><br><span class="line">    </span><br><span class="line">    </span><br><span class="line"><span class="number">5.</span><span class="keyword">for</span>循环里面的内部原理</span><br><span class="line">	names = [<span class="string">&#x27;egon&#x27;</span>, <span class="string">&#x27;tom&#x27;</span>, <span class="string">&#x27;jack&#x27;</span>, <span class="string">&#x27;lili&#x27;</span>, <span class="string">&#x27;ttt&#x27;</span>]</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    iter_s1 = <span class="built_in">iter</span>(names)</span><br><span class="line">    <span class="built_in">print</span>(<span class="built_in">next</span>(iter_s1))</span><br><span class="line">    <span class="built_in">print</span>(<span class="built_in">next</span>(iter_s1))</span><br><span class="line">    <span class="built_in">print</span>(<span class="built_in">next</span>(iter_s1))</span><br><span class="line">    <span class="built_in">print</span>(<span class="built_in">next</span>(iter_s1))</span><br><span class="line">    <span class="built_in">print</span>(<span class="built_in">next</span>(iter_s1))</span><br><span class="line"></span><br><span class="line">    iter_s2=<span class="built_in">iter</span>(names)</span><br><span class="line"></span><br><span class="line">    <span class="built_in">print</span>(<span class="built_in">next</span>(iter_s2))</span><br><span class="line">    <span class="built_in">print</span>(<span class="built_in">next</span>(iter_s2))</span><br><span class="line">    <span class="built_in">print</span>(<span class="built_in">next</span>(iter_s2))</span><br><span class="line">    <span class="built_in">print</span>(<span class="built_in">next</span>(iter_s2))</span><br><span class="line">    <span class="built_in">print</span>(<span class="built_in">next</span>(iter_s2))</span><br><span class="line"></span><br><span class="line"><span class="comment"># 下面的第二个for循环其实是取不出值的</span></span><br><span class="line">    iter_names = <span class="built_in">iter</span>(names)</span><br><span class="line">    <span class="keyword">for</span> x <span class="keyword">in</span> iter_names :</span><br><span class="line">        <span class="built_in">print</span>(x)</span><br><span class="line"></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;=&#x27;</span>*<span class="number">50</span>)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> x <span class="keyword">in</span> iter_names :</span><br><span class="line">        <span class="built_in">print</span>(x)</span><br></pre></td></tr></table></figure>



<h2 id="十五、生成器-amp-yield"><a href="#十五、生成器-amp-yield" class="headerlink" title="十五、生成器&amp;yield"></a>十五、生成器&amp;yield</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 什么是生成器</span><br><span class="line">	生成器就是自定义的迭代器</span><br><span class="line"><span class="number">2.</span> 为什么要用生成器</span><br><span class="line">	因为生成器也就是迭代器是惰性计算不浪费内存，所以我们有时候可以自己自定义一个迭代器</span><br><span class="line"><span class="number">3.</span> 如何定义生成器</span><br><span class="line">	<span class="comment"># 这个就得用到我们的秘籍-----yield</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">func</span>():</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;one...&#x27;</span>)</span><br><span class="line">    <span class="keyword">yield</span> <span class="number">111</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;two...&#x27;</span>)</span><br><span class="line">    <span class="keyword">yield</span> <span class="number">222</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;three...&#x27;</span>)</span><br><span class="line">    <span class="keyword">yield</span> <span class="number">333</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;fourth&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 函数内但凡出现yield关键字，再调用函数不会立即执行函数体代码，而会返回一个</span></span><br><span class="line"><span class="comment"># 生成器对象，生成器对象本质就是一个自定义的迭代器对象</span></span><br><span class="line"></span><br><span class="line"><span class="number">4.</span> <span class="keyword">yield</span> 对比 <span class="keyword">return</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">相同点：都可以返回任意类型、任意个数的值</span></span><br><span class="line"><span class="string">不同点：return只能返回值一次值，函数就立即结束</span></span><br><span class="line"><span class="string">       yield可以返回多次值，yield可以暂停函数的运行</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br></pre></td></tr></table></figure>

<h2 id="十六、函数递归"><a href="#十六、函数递归" class="headerlink" title="十六、函数递归"></a>十六、函数递归</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 什么是函数递归</span><br><span class="line">	就是定义一个函数，然后再函数内部调用自己，说白了就是个循环</span><br><span class="line"><span class="number">2.</span> 函数递归可以分为两个阶段</span><br><span class="line">    <span class="number">2.1</span> 回溯 就是去要的过程</span><br><span class="line">    <span class="number">2.2</span> 递推 要到了回来的过程</span><br><span class="line"><span class="number">3.</span> 函数递归应用</span><br><span class="line">    l = [<span class="number">1</span>, [<span class="number">2</span>, [<span class="number">3</span>, [<span class="number">4</span>, [<span class="number">5</span>, [<span class="number">6</span>, [<span class="number">7</span>, [<span class="number">8</span>]]]]]]]]</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">func</span>(<span class="params">l</span>):</span></span><br><span class="line">        <span class="keyword">for</span> items <span class="keyword">in</span> l:</span><br><span class="line">            <span class="keyword">if</span> <span class="built_in">type</span>(items) <span class="keyword">is</span> <span class="built_in">list</span>:</span><br><span class="line">                func(items)</span><br><span class="line">            <span class="keyword">else</span>:</span><br><span class="line">                <span class="built_in">print</span>(items)</span><br><span class="line"></span><br><span class="line">    func(l)</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h2 id="十七、三元表达式"><a href="#十七、三元表达式" class="headerlink" title="十七、三元表达式"></a>十七、三元表达式</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 过于麻烦的普通代码</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">func</span>():</span></span><br><span class="line">    <span class="keyword">if</span> x &gt; y:</span><br><span class="line">        <span class="keyword">return</span> x</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        <span class="keyword">return</span> y</span><br><span class="line"><span class="comment"># 三元表达式</span></span><br><span class="line">  x <span class="keyword">if</span> x &gt; y <span class="keyword">else</span> y</span><br><span class="line">  </span><br><span class="line"><span class="comment"># 三元表达式的格式</span></span><br><span class="line">达成条件的返回的值 <span class="keyword">if</span> 条件 条件没有达成的返回值</span><br><span class="line">     </span><br></pre></td></tr></table></figure>



<h2 id="十八、各种生成式"><a href="#十八、各种生成式" class="headerlink" title="十八、各种生成式"></a>十八、各种生成式</h2><h3 id="18-1-列表生成式"><a href="#18-1-列表生成式" class="headerlink" title="18.1 列表生成式"></a>18.1 列表生成式</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">列表生成式</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="comment"># 正常思路</span></span><br><span class="line">l = [<span class="string">&quot;xxx_dsb&quot;</span>, <span class="string">&quot;aaa_dsb&quot;</span>, <span class="string">&quot;bbb_dsb&quot;</span>, <span class="string">&quot;ccc_dsb&quot;</span>, <span class="string">&quot;yuki&quot;</span>]</span><br><span class="line">new_l = []</span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> l:</span><br><span class="line">    <span class="keyword">if</span> i.endswith(<span class="string">&quot;dsb&quot;</span>):</span><br><span class="line">        new_l.append(i)</span><br><span class="line"><span class="built_in">print</span>(new_l)</span><br><span class="line"><span class="comment"># 列表生成式</span></span><br><span class="line">l = [<span class="string">&quot;xxx_dsb&quot;</span>, <span class="string">&quot;aaa_dsb&quot;</span>, <span class="string">&quot;bbb_dsb&quot;</span>, <span class="string">&quot;ccc_dsb&quot;</span>, <span class="string">&quot;yuki&quot;</span>]</span><br><span class="line">new_l = [i <span class="keyword">for</span> i <span class="keyword">in</span> l <span class="keyword">if</span> i.endswith(<span class="string">&quot;dsb&quot;</span>)]</span><br><span class="line"><span class="built_in">print</span>(new_l)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 练习 1 把所有小写字母换成大写字母  2 把所有名字去掉后缀</span></span><br><span class="line">l = [<span class="string">&quot;xxx_dsb&quot;</span>, <span class="string">&quot;aaa_dsb&quot;</span>, <span class="string">&quot;bbb_dsb&quot;</span>, <span class="string">&quot;ccc_dsb&quot;</span>, <span class="string">&quot;yuki&quot;</span>]</span><br><span class="line">new_1 = [name.upper() <span class="keyword">for</span> name <span class="keyword">in</span> l]</span><br><span class="line">new_l = [i.replace(<span class="string">&quot;_dsb&quot;</span>, <span class="string">&#x27;&#x27;</span>) <span class="keyword">for</span> i <span class="keyword">in</span> l <span class="keyword">if</span> <span class="string">&quot;_dsb&quot;</span> <span class="keyword">in</span> i]</span><br></pre></td></tr></table></figure>

<h3 id="18-2-字典生成式"><a href="#18-2-字典生成式" class="headerlink" title="18.2 字典生成式"></a>18.2 字典生成式</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">keys = &#123;<span class="string">&quot;aaa&quot;</span>, <span class="string">&#x27;bbb&#x27;</span>, <span class="string">&#x27;ccc&#x27;</span>&#125;</span><br><span class="line">&#123;k: <span class="literal">None</span> <span class="keyword">for</span> k <span class="keyword">in</span> keys&#125;</span><br></pre></td></tr></table></figure>



<h3 id="18-3-集合生成式"><a href="#18-3-集合生成式" class="headerlink" title="18.3  集合生成式"></a>18.3  集合生成式</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">keys = &#123;<span class="string">&quot;aaa&quot;</span>, <span class="string">&#x27;bbb&#x27;</span>, <span class="string">&#x27;ccc&#x27;</span>&#125;</span><br><span class="line">&#123;k <span class="keyword">for</span> k <span class="keyword">in</span> keys&#125;</span><br></pre></td></tr></table></figure>

<h3 id="18-4-生成器生成式"><a href="#18-4-生成器生成式" class="headerlink" title="18.4 生成器生成式"></a>18.4 生成器生成式</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 把一个文件所有的字符总和取出来</span></span><br><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(<span class="string">&quot;stock_data.txt&quot;</span>, mode=<span class="string">&quot;rt&quot;</span>, encoding=<span class="string">&quot;UTF-8&quot;</span>) <span class="keyword">as</span> file:</span><br><span class="line">   <span class="built_in">print</span>(<span class="built_in">sum</span>(<span class="built_in">len</span>(line) <span class="keyword">for</span> line <span class="keyword">in</span> file)  <span class="comment"># 节约了内存 提高了效率</span></span><br></pre></td></tr></table></figure>

<h2 id="十九、-二分法"><a href="#十九、-二分法" class="headerlink" title="十九、 二分法"></a>十九、 二分法</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line">需求：如果一个列表储存着从小到大的数字</span><br><span class="line">	然后从中需要找一个数字</span><br><span class="line">    </span><br><span class="line"><span class="number">1.</span><span class="keyword">for</span>循环</span><br><span class="line">	<span class="built_in">list</span> = [-<span class="number">6</span>, -<span class="number">1</span>, <span class="number">2</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">10</span>, <span class="number">12</span>, <span class="number">16</span>, <span class="number">20</span>, <span class="number">25</span>, <span class="number">28</span>, <span class="number">30</span>, <span class="number">65</span>, <span class="number">98</span>]</span><br><span class="line">    need_num = <span class="number">25</span></span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">list</span>:</span><br><span class="line">        <span class="keyword">if</span> i == need_num:</span><br><span class="line">            <span class="keyword">break</span></span><br><span class="line">    <span class="built_in">print</span>(i)</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">这样是可以找出来，但是这样假如列表有很多元素，比如一千万个</span></span><br><span class="line"><span class="string">这样寻找会效率非常低，所以应该改进</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="number">2.</span>二分法  </span><br><span class="line"><span class="comment"># 思路模板</span></span><br><span class="line"><span class="built_in">list</span> = [-<span class="number">6</span>, -<span class="number">1</span>, <span class="number">2</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">10</span>, <span class="number">12</span>, <span class="number">16</span>, <span class="number">20</span>, <span class="number">25</span>, <span class="number">28</span>, <span class="number">30</span>, <span class="number">65</span>, <span class="number">98</span>]</span><br><span class="line">need_num = <span class="number">25</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">func</span>():</span></span><br><span class="line">    median = 中间值</span><br><span class="line">    <span class="keyword">if</span> 中间值 &gt; need_num：</span><br><span class="line">        就要从左边开始寻找</span><br><span class="line">        <span class="built_in">list</span> = 左边的列表</span><br><span class="line">        <span class="function"><span class="keyword">def</span> <span class="title">func</span>()</span></span><br><span class="line"><span class="function">    <span class="title">elif</span> 中间值 &lt; <span class="title">need_num</span>:</span></span><br><span class="line">        就要从右边开始寻找</span><br><span class="line">        <span class="built_in">list</span> = 右边的列表</span><br><span class="line">        <span class="function"><span class="keyword">def</span> <span class="title">func</span>()</span></span><br><span class="line"><span class="function">    <span class="title">else</span>：</span></span><br><span class="line"><span class="function">        <span class="title">print</span>(<span class="params">find it</span>)</span></span><br><span class="line"><span class="function">        </span></span><br><span class="line"><span class="function"># 实现</span></span><br><span class="line"><span class="function"><span class="title">list</span> = [-6, -1, 2, 5, 6, 10, 12, 16, 20, 25, 28, 30, 65, 98]</span></span><br><span class="line"><span class="function"><span class="title">need_num</span> = 30</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"><span class="title">def</span> <span class="title">func</span>(<span class="params">need_num, <span class="built_in">list</span></span>):</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="built_in">list</span>)</span><br><span class="line">    median = <span class="built_in">len</span>(<span class="built_in">list</span>) // <span class="number">2</span></span><br><span class="line">    <span class="keyword">if</span> <span class="built_in">list</span>[median] &gt; need_num:</span><br><span class="line">        <span class="built_in">list</span> = <span class="built_in">list</span>[<span class="number">0</span>:median]</span><br><span class="line">        <span class="built_in">print</span>(<span class="built_in">list</span>)</span><br><span class="line">        func(need_num, <span class="built_in">list</span>)</span><br><span class="line">    <span class="keyword">elif</span> <span class="built_in">list</span>[median] &lt; need_num:</span><br><span class="line">        <span class="built_in">list</span> = <span class="built_in">list</span>[median:]</span><br><span class="line">        <span class="built_in">print</span>(<span class="built_in">list</span>)</span><br><span class="line">        func(need_num, <span class="built_in">list</span>)</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;find it&quot;</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">func(need_num, <span class="built_in">list</span>)</span><br></pre></td></tr></table></figure>



<h2 id="二十、匿名函数"><a href="#二十、匿名函数" class="headerlink" title="二十、匿名函数"></a>二十、匿名函数</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">匿名函数的应用场景</span><br><span class="line">	任何只需要用到一次函数，用完即可不要 就可以用匿名函数</span><br><span class="line"><span class="keyword">lambda</span> x,y: x+y</span><br><span class="line"></span><br><span class="line">匿名函数与有名函数有相同的作用域，但是匿名意味着引用计数为<span class="number">0</span>，使用一次就释放，所以匿名函数用于临时使用一次的场景，匿名函数通常与其他函数配合使用，我们以下述字典为例来介绍它</span><br><span class="line"></span><br><span class="line">salaries=&#123;</span><br><span class="line">    <span class="string">&#x27;siry&#x27;</span>:<span class="number">3000</span>,</span><br><span class="line">    <span class="string">&#x27;tom&#x27;</span>:<span class="number">7000</span>,</span><br><span class="line">    <span class="string">&#x27;lili&#x27;</span>:<span class="number">10000</span>,</span><br><span class="line">    <span class="string">&#x27;jack&#x27;</span>:<span class="number">2000</span></span><br><span class="line">&#125;</span><br><span class="line">要想取得薪水的最大值，我们可以使用内置函数<span class="built_in">max</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">max</span>(salaries)</span><br><span class="line"><span class="string">&#x27;tom&#x27;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">min</span>(salaries)</span><br><span class="line"><span class="string">&#x27;jack&#x27;</span></span><br><span class="line">内置<span class="built_in">max</span>和<span class="built_in">min</span>都支持迭代器协议，工作原理都是迭代字典，取得是字典的键，因而比较的是键的最大和最小值，而我们想要的是比较值的最大值与最小值，于是做出如下改动</span><br><span class="line"><span class="comment"># 函数max会迭代字典salaries，每取出一个“人名”就会当做参数传给指定的匿名函数，然后将匿名函数的返回值当做比较依据，最终返回薪资最高的那个人的名字</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">max</span>(salaries,key=<span class="keyword">lambda</span> k:salaries[k]) </span><br><span class="line"><span class="string">&#x27;lili&#x27;</span></span><br><span class="line"><span class="comment"># 原理同上</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">min</span>(salaries,key=<span class="keyword">lambda</span> k:salaries[k])</span><br><span class="line"><span class="string">&#x27;jack&#x27;</span></span><br><span class="line">同理，我们直接对字典进行排序，默认也是按照字典的键去排序的</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">sorted</span>(salaries)</span><br><span class="line">[<span class="string">&#x27;jack&#x27;</span>, <span class="string">&#x27;lili&#x27;</span>, <span class="string">&#x27;siry&#x27;</span>, <span class="string">&#x27;tom&#x27;</span>]</span><br></pre></td></tr></table></figure>


            </div>

            <!-- Post Comments -->
            
    <!-- 使用 DISQUS_CLICK -->
<div id="disqus-comment">
    <div id="disqus_thread"></div>

<!-- add animation -->
<style>
	.disqus_click_btn {
            line-height: 30px;
            margin: 0;
            min-width: 50px;
            padding: 0 14px;
            display: inline-block;
            font-family: "Roboto", "Helvetica", "Arial", sans-serif;
            font-size: 14px;
            font-weight: 400;
            text-transform: uppercase;
            letter-spacing: 0;
            overflow: hidden;
            will-change: box-shadow;
            transition: box-shadow .2s cubic-bezier(.4, 0, 1, 1), background-color .2s cubic-bezier(.4, 0, .2, 1), color .2s cubic-bezier(.4, 0, .2, 1);
            outline: 0;
            cursor: pointer;
            text-decoration: none;
            text-align: center;
            vertical-align: middle;
            border: 0;
            background: rgba(158, 158, 158, .2);
            box-shadow: 0 2px 2px 0 rgba(0, 0, 0, .14), 0 3px 1px -2px rgba(0, 0, 0, .2), 0 1px 5px 0 rgba(0, 0, 0, .12);
            color: #fff;
            background-color: #7EC0EE;
            text-shadow: 0
        }
</style>
	
<div class="btn_click_load" id="disqus_bt"> 
    <button class="disqus_click_btn">点击查看评论</button>
</div>

<!--
<script type="text/javascript">
$('.btn_click_load').click(function() {
    /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
    var disqus_shortname = 'http-miccall-tech'; // required: replace example with your forum shortname

    /* * * DON'T EDIT BELOW THIS LINE * * */
    (function() {
      var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
      dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
      (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
    })();

    document.getElementById('disqus_bt').style.display = "none";
});
</script>
-->
<script type="text/javascript">
    var disqus_config = function () {
        this.page.url = 'https://gitee.com/AmeX/amelin-ting.git/2017/03/29/%E5%87%BD%E6%95%B0/';  // Replace PAGE_URL with your page's canonical URL variable
        this.page.identifier = 'https://gitee.com/AmeX/amelin-ting.git/2017/03/29/%E5%87%BD%E6%95%B0/'; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
    };
</script>

<script type="text/javascript">
    $('.btn_click_load').click(function() {  //click to load comments
        (function() { // DON'T EDIT BELOW THIS LINE
            var d = document;
            var s = d.createElement('script');
            s.src = '//http-miccall-tech.disqus.com/embed.js';
            s.setAttribute('data-timestamp', + new Date());
            (d.head || d.body).appendChild(s);
        })();
        $('.btn_click_load').css('display','none');
    });
</script>
</div>
<style>
    #disqus-comment{
        background-color: #eee;
        padding: 2pc;
    }
</style>


        </div>
        <!-- Copyright 版权 start -->
                <div id="copyright">
            <ul>
                <li>&copy;Powered By <a target="_blank" rel="noopener" href="https://hexo.io/zh-cn/" style="border-bottom: none;">hexo</a></li>
                <li>Design: <a target="_blank" rel="noopener" href="http://miccall.tech " style="border-bottom: none;">miccall</a></li>
            </ul>
            
                <span id="busuanzi_container_site_pv">本站总访问量<span id="busuanzi_value_site_pv"></span>次</span>
			
        </div>
    </div>
</body>



 	
</html>
